<template>
  <view class="link-wifi">
    <nav-bar :title="title" ref="navBarRef" />
    <link-wi-fi v-if="page === 'link-wifi'" ref="linkWiFiRef" />
    <bind-wi-fi v-else ref="bindWiFiRef" />
  </view>
</template>

<script>
import linkWiFi from "./components/link-wifi";
import bindWiFi from "./components/bind-wifi";
import { isBindByDeviceNo } from "@/service/api/sp-store/wifi";
import jumpAdvertisement from "@/pages/sp-store/mixin/link-wifi/jumpAdvertisement";
import { mapState } from "vuex";
import { getUrlQuery } from "@/utils/format";
import handleEventTrack from "@/mixin/handleEventTrack";
import {
  EVENT_TRACK_EVENT_OF_RESULT_FOR_WIFI,
  EVENT_TRACK_TYPE_OF_BUSINESS_FOR_WIFI,
} from "@/pages/sp-store/const/wifi";

export default {
  mixins: [jumpAdvertisement, handleEventTrack],
  components: { linkWiFi, bindWiFi },
  computed: {
    ...mapState("sp-common/auth", ["app_token"]),
    ...mapState("sp-common/base", ["themeColor"]),
    title({ page }) {
      return page === "link-wifi" ? "连接WiFi" : "绑定WiFi";
    },
  },
  data() {
    return {
      page: "link-wifi",
    };
  },
  created() {},
  destroyed() {},
  onShow() {
    this.options = this.$Route.query;
    this.$refs.navBarRef?.isShowBack();
  },
  onLoad(options) {
    console.log("参数", options);
    this.options = options;
    // WiFi码牌直连
    this.innit();
  },
  methods: {
    async handleGetToken() {
      await this.$store.dispatch("sp-common/auth/userLogin");
      console.log("触发一次调用token", this.app_token);
      if (!this.app_token) {
        this.$nextTick(() => {
          this.handleGetToken();
        });
      }
    },
    innit() {
      const options = JSON.parse(JSON.stringify(this.options));
      if (options) {
        if (options?.q) {
          //从WiFi码进来的
          const q = decodeURIComponent(options.q); // 获取到二维码原始链接内容
          const q1 = decodeURIComponent(q); // 获取到二维码原始链接内容
          this.urlQuery = getUrlQuery(q1);
        }
        this.innitWifiData().then(() => {
          setTimeout(() => {
            this.handleGetToken();
          }, 1000);
        });
      }
    },
    async innitWifiData() {
      const res = await isBindByDeviceNo({
        deviceNo:
          this.urlQuery?.code || this.options?.code || this.options?.deviceNo,
      }).catch((err) => {
        console.log("获取WiFi码牌信息失败" + JSON.stringify(err));
        // this.handleEventTrack({
        //   sign: "wifi",
        //   ...this.options,
        //   ...this.urlQuery,
        //   opType: EVENT_TRACK_TYPE_OF_BUSINESS_FOR_WIFI.INIT,
        //   status: EVENT_TRACK_EVENT_OF_RESULT_FOR_WIFI.BUSINESS_FAIL,
        //   failReason: "获取WiFi码牌信息失败" + JSON.stringify(err),
        // }); //数据埋点
      });
      if (res) {
        //此时是被人绑定过的,直连WiFi
        this.options = {
          ...res.wifiConfigVo,
        };
        this.options.name = res?.wifiConfigVo?.wifiName;
        this.options.password = res?.wifiConfigVo?.password;
        this.page = "link-wifi";
        this.$nextTick(() => {
          this.$refs.linkWiFiRef.handleReceive(this.options);
        });
      } else {
        console.log("未绑定，需要跳到绑定WiFi");
        this.page = "bind-wifi";
        this.$nextTick(() => {
          this.$refs.bindWiFiRef.handleReceive(this.options);
        });
      }
      console.log("获取WiFi码牌信息成功", res);
      // this.handleEventTrack({
      //   sign: "wifi",
      //   ...this.options,
      //   ...this.urlQuery,
      //   deviceNo:
      //     this.urlQuery?.code || this.options?.code || this.options?.deviceNo,
      //   opType: EVENT_TRACK_TYPE_OF_BUSINESS_FOR_WIFI.INIT,
      //   status: EVENT_TRACK_EVENT_OF_RESULT_FOR_WIFI.BUSINESS_SUCCESS,
      //   failReason: "获取WiFi码牌信息成功",
      // }); //数据埋点
    },
  },
};
</script>

<style scoped lang="scss"></style>
